package com.open.capacity.forum.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.CodeEnum;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.forum.dao.ArticleCategoryDao;
import com.open.capacity.forum.model.ArticleCategory;
import com.open.capacity.forum.service.ArticleCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ArticleCategoryServiceImpl extends ServiceImpl<ArticleCategoryDao, ArticleCategory> implements ArticleCategoryService {
    @Override
    public List<ArticleCategory> findArticleCategoryByUserId(Long userId) throws ServiceException {
        try {
            return lambdaQuery().eq(ArticleCategory::getUserid, userId).list();
        } catch (Exception e) {
            log.warn(e.getMessage());
            throw new ServiceException(e);
        }
    }

    @Override
    public PageResult findArticleCategoryPage(Map<String, Object> params) {
        PageUtil.pagePreProcess(params);
        List<ArticleCategory> categories = baseMapper.selectArticleCategoryByCondition(params);
        PageInfo<ArticleCategory> pageInfo = new PageInfo<>(categories);
        return PageResult.<ArticleCategory>builder().code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }
}
